package com.ruoyi.merchant.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.merchant.domain.CodeItems;
import com.ruoyi.merchant.service.ICodeItemsService;
import com.ruoyi.merchant.util.EntitiesDeleteCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 基础数据代码项（此主要存储相关数据字典信息）Controller
 * 
 * @author xin.jucheng
 * @date 2024-03-28
 */
@Slf4j
@RestController
@RequestMapping("/items")
public class CodeItemsController extends BaseController
{
    @Autowired
    private ICodeItemsService codeItemsService;

    /**
     * 查询基础数据代码项（此主要存储相关数据字典信息）列表
     */
    @RequiresPermissions("system:items:list")
    @GetMapping("/list")
    public TableDataInfo list(CodeItems codeItems)
    {
        startPage();
        List<CodeItems> list = codeItemsService.selectCodeItemsList(codeItems);
        return getDataTable(list);
    }

    /**
     * 查询正常字典列表信息  pid 、layer   by pid
     */
    @GetMapping(value = "/listAllBypid")
    public AjaxResult listAllBypid(@RequestParam("setId") String setId) {
        List<CodeItems> itemsList = null;
        try{

            if (StringUtils.isBlank(setId)){
                return error("参数不能为空！");
            }

            CodeItems codeItems = new CodeItems();
            codeItems.setDel(EntitiesDeleteCodeEnum.OK.getCode());
            codeItems.setSetId(setId);
            List<CodeItems> codeItemsList= codeItemsService.selectCodeItemsList(codeItems);

            if (CollectionUtils.isEmpty(codeItemsList)) {
                return error("根据pid查询正常字典列表信息失败[" + codeItems.getSetId() + "]");
            }

            CodeItems items = codeItemsList.get(0);
            codeItems.setPid(items.getId());
            codeItems.setSetId(null);
            itemsList =  codeItemsService.selectCodeItemsList(codeItems);

        }catch (Exception e){
            log.error("listAllBypid",e);
            return error("根据pid查询正常字典列表信息失败");
        }
        return success(itemsList);
    }

    /**
     * 导出基础数据代码项（此主要存储相关数据字典信息）列表
     */
    @RequiresPermissions("system:items:export")
    @Log(title = "基础数据代码项（此主要存储相关数据字典信息）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CodeItems codeItems)
    {
        List<CodeItems> list = codeItemsService.selectCodeItemsList(codeItems);
        ExcelUtil<CodeItems> util = new ExcelUtil<CodeItems>(CodeItems.class);
        util.exportExcel(response, list, "基础数据代码项（此主要存储相关数据字典信息）数据");
    }

    /**
     * 获取基础数据代码项（此主要存储相关数据字典信息）详细信息
     */
    @RequiresPermissions("system:items:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(codeItemsService.selectCodeItemsById(id));
    }

    /**
     * 新增基础数据代码项（此主要存储相关数据字典信息）
     */
    @RequiresPermissions("system:items:add")
    @Log(title = "基础数据代码项（此主要存储相关数据字典信息）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CodeItems codeItems)
    {
        return toAjax(codeItemsService.insertCodeItems(codeItems));
    }

    /**
     * 修改基础数据代码项（此主要存储相关数据字典信息）
     */
    @RequiresPermissions("system:items:edit")
    @Log(title = "基础数据代码项（此主要存储相关数据字典信息）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CodeItems codeItems)
    {
        return toAjax(codeItemsService.updateCodeItems(codeItems));
    }

    /**
     * 删除基础数据代码项（此主要存储相关数据字典信息）
     */
    @RequiresPermissions("system:items:remove")
    @Log(title = "基础数据代码项（此主要存储相关数据字典信息）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(codeItemsService.deleteCodeItemsByIds(ids));
    }
}
